﻿

namespace Mulala.Cavan.Application.Seedwork
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web;
    using Mulala.Cavan.Domain.MainBoundedContext.Setting;
    using Mulala.Cavan.Core.Helper;
    using Mulala.Cavan.Core.Extension;
    using Mulala.Cavan.Domain.MainBoundedContext.SecurityModule;

    public static class CavanWebUtility
    {
        public static string GetUserIP()
        {
            string userIP;
            if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] == null)
            {
                userIP = System.Web.HttpContext.Current.Request.UserHostAddress;
            }
            else
            {
                userIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            }
            return userIP;
        }

        public static string GetUserSelectedCity()
        {
            if (!CookieHelper.IsExsit(Project.Settings.MulalaHistoryCookieName))
            {
                CookieHelper.CreateCookie(Project.Settings.MulalaHistoryCookieName);
                CookieHelper.SetCookie(MulalaAppConst.CITY, Project.Settings.DefaultArea.ID, Project.Settings.MulalaHistoryCookieName);
            }

            string city = string.Empty;
            if (CookieHelper.IsExsit(Project.Settings.MulalaHistoryCookieName))
            {
                city = CookieHelper.GetCookie(MulalaAppConst.CITY, Project.Settings.MulalaHistoryCookieName);
                if (city.IsEmpty())
                {
                    CookieHelper.SetCookie(MulalaAppConst.CITY, Project.Settings.DefaultArea.ID, Project.Settings.MulalaHistoryCookieName);
                }
            }
            return city.IsNotEmpty() ? city : Project.Settings.DefaultArea.ID;
        }

        public static string GetUnReadNote(SecurityIdentifier.Role role)
        {
            if (!CookieHelper.IsExsit(Project.Settings.MulalaHistoryCookieName))
            {
                return "0";
            }
            if (role == SecurityIdentifier.Role.Corporation)
            {
               return  CookieHelper.GetCookie(MulalaAppConst.CORPORATIONUNREADNOTE, Project.Settings.MulalaHistoryCookieName);
            }
            return "0";
        }
    }
}
